package com.autumn.common.api.application.dto.output;

import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * 分页查询结果
 * 
 * @author 老码农
 *
 *         2017-10-31 09:33:42
 */
public class PageQueryResult<T> implements IPageQueryResult<T> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7123565605554847952L;
	@JSONField(ordinal=1)
	private final int currentPage;
	@JSONField(ordinal=2)
	private final int pageSize;
	@JSONField(ordinal=3)
	private final int pageTotal;
	@JSONField(ordinal=4)
	private final int rowTotal;
	@JSONField(ordinal=5)
	private List<T> items;

	/**
	 * 实例化页查询结果
	 * 
	 * @param currentPage
	 *            当前页
	 * @param pageSize
	 *            页大小
	 * @param rowTotal
	 *            行总数
	 */
	public PageQueryResult(int currentPage, int pageSize, int rowTotal) {
		if (pageSize < 1) {
			pageSize = 1;
		}
		if (rowTotal < 0) {
			rowTotal = 0;
		}
		this.pageSize = pageSize;
		this.rowTotal = rowTotal;
		int count = this.rowTotal / this.pageSize;
		if (this.rowTotal % this.pageSize != 0) {
			count++;
		}
		if (count < 1) {
			count = 1;
		}
		this.pageTotal = count;
		if (currentPage < 1) {
			currentPage = 1;
		} else if (currentPage > this.pageTotal) {
			currentPage = this.pageTotal;
		}
		this.currentPage = currentPage;
	}

	/**
	 * 获取当前页
	 * 
	 * @return
	 */
	@Override
	public int getCurrentPage() {
		return this.currentPage;
	}

	/**
	 * 获取页大小
	 * 
	 * @return
	 */
	@Override
	public int getPageSize() {
		return this.pageSize;
	}

	/**
	 * 获取页总数
	 * 
	 * @return
	 */
	@Override
	public int getPageTotal() {
		return this.pageTotal;
	}

	/**
	 * 获取行总数
	 * 
	 * @return
	 */
	@Override
	public int getRowTotal() {
		return this.rowTotal;
	}

	/**
	 * 获取项目集合
	 * 
	 * @return
	 */
	@Override
	public List<T> getItems() {
		return items;
	}

	/**
	 * 设置项目集合
	 * 
	 * @param items
	 */	
	public void setItems(List<T> items) {
		this.items = items;
	}

}
